/*
*
* Copyright (c) 2011 Vaulting Systems International
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy 
* of this software and associated documentation files (the "Software"), to deal 
* in the Software without restriction, including without limitation the rights 
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
* of the Software, and to permit persons to whom the Software is furnished to do  
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all  
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE  
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.securecreditlib.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

import com.ekeyman.securecreditlib.business.RegistryBO;
import com.ekeyman.securecreditlib.domain.Registry;
import com.ekeyman.securecreditlib.service.EkeymanService;
import com.ekeymanlib.dto.AppDeviceApiKeys;

public class RegisterAppDeviceUT {
	
	private static final String[] SPRING_CONFIG_FILES = new String[]{"applicationContext-lib.xml"};
	ClassPathXmlApplicationContext ctx;
	
	private EkeymanService ekeymanService;
	private RegistryBO registryBO;
	
	@Test
	public void testRegisterAppDevice() {
		
		System.out.println("testRegisterAppDevice entered...");
		
		Registry registry = getRegistryBO().getRegistry();
		
		if(registry == null || registry.getPublicKey() == null  || registry.getPublicKey().equalsIgnoreCase("")){
				
			String apiKey = "a02023ba-690b-493a-aee7-85d1092914b0";
			String application = "SecureCredit";
			String device = "internal";
			
			AppDeviceApiKeys appDeviceApiKeys = ekeymanService.registerAppDevice(
					apiKey, application, device);
			
			registry = new Registry();
			registry.setPublicKey(appDeviceApiKeys.getPublicKey());
			registry.setPrivateKey(appDeviceApiKeys.getPrivateKey());
			getRegistryBO().saveRegistry(registry);
		}
		
		
		System.out.println("testRegisterAppDevice...publicKey: " + registry.getPublicKey());
		System.out.println("testRegisterAppDevice...privateKey: " + registry.getPrivateKey());		
		
		System.out.println("testRegisterAppDevice exited...");
	}
	
	
	@BeforeSuite
	public void beforeSuite() {
		System.out.println("Before suite...");
		
		ctx = new ClassPathXmlApplicationContext(SPRING_CONFIG_FILES);
		setEkeymanService((EkeymanService)ctx.getBeanFactory().getBean("ekeymanService"));
		setRegistryBO((RegistryBO)ctx.getBeanFactory().getBean("registryBO"));
	}
	
	@AfterSuite
	public void afterSuite() {
		System.out.println("After suite...");
	}


	public EkeymanService getEkeymanService() {
		return ekeymanService;
	}


	public void setEkeymanService(EkeymanService ekeymanService) {
		this.ekeymanService = ekeymanService;
	}


	public void setRegistryBO(RegistryBO registryBO) {
		this.registryBO = registryBO;
	}


	public RegistryBO getRegistryBO() {
		return registryBO;
	}
	


}
